package com.twetec.cms.service.system.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.twetec.cms.common.constant.*;
import com.twetec.cms.dao.mapper.system.CmsMenuMapper;
import com.twetec.cms.dao.mapper.system.CmsRoleMenuMapper;
import com.twetec.cms.dao.mapper.system.CmsUserRoleMapper;
import com.twetec.cms.dao.mapper.ucenter.UserMapper;
import com.twetec.cms.dao.mapper.website.ColumnMapper;
import com.twetec.cms.dao.mapper.website.SiteMapper;
import com.twetec.cms.model.po.system.CmsMenuPO;
import com.twetec.cms.model.po.system.CmsUserRolePO;
import com.twetec.cms.model.po.website.ColumnPO;
import com.twetec.cms.model.po.website.SitePO;
import com.twetec.cms.model.vo.website.CmsMenuPermissionListVO;
import com.twetec.cms.model.vo.website.CmsMenuPermissionTreeVO;
import com.twetec.cms.service.system.CmsMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: zhaichangzheng
 * @Date: 2022/2/8 14:40 （日期和时间）
 */
@Service
public class CmsMenuServiceImpl  extends ServiceImpl<CmsMenuMapper, CmsMenuPO> implements CmsMenuService {


    @Autowired
    private SiteMapper siteMapper;

    @Autowired
    private CmsUserRoleMapper userRoleMapper;

    @Autowired
    private CmsRoleMenuMapper cmsRoleMenuMapper;

    /**
     * 添加站点的菜单权限
     * @param siteId
     * @param siteName
     * @return
     */
    @Override
    public ArrayList<CmsMenuPO> addSitePermission(Long siteId, String siteName, Long userId) {

        // 添加站点权限

        // 站点拥有的权限列表
        ArrayList<CmsMenuPO> cmsMenuPOS = new ArrayList<>();

        // 添加站点（目录）
        CmsMenuPO cmsMenuSitePO = new CmsMenuPO();
        long cmsMenuSiteId = IdWorker.getId();
        cmsMenuSitePO.setId(cmsMenuSiteId);
        cmsMenuSitePO.setName(siteName);
        cmsMenuSitePO.setPid(0L);
        cmsMenuSitePO.setPids("[0],");
        cmsMenuSitePO.setSiteId(siteId);
        cmsMenuSitePO.setCreatorId(userId);
        cmsMenuSitePO.setModifierId(userId);
        cmsMenuSitePO.setType(CmsMenuPermissionTypeEnum.ZHAN_DIAN.getValue());
        cmsMenuPOS.add(cmsMenuSitePO);

        // 添加栏目（菜单）
        CmsMenuPO cmsMenuColumnPO = new CmsMenuPO();
        long cmsMenuColumnId = IdWorker.getId();
        cmsMenuColumnPO.setId(cmsMenuColumnId);
        cmsMenuColumnPO.setName("栏目");
        cmsMenuColumnPO.setPid(cmsMenuSiteId);
        cmsMenuColumnPO.setPids("[0]," + "[" + cmsMenuSiteId + "],");
        cmsMenuColumnPO.setType(CmsMenuPermissionTypeEnum.CAI_DAN.getValue());
        cmsMenuColumnPO.setSiteId(siteId);
        cmsMenuColumnPO.setCreatorId(userId);
        cmsMenuColumnPO.setModifierId(userId);
        cmsMenuPOS.add(cmsMenuColumnPO);

        // 获取站点栏目拥有的权限
        CmsSiteColumnPermissionEnum[] values = CmsSiteColumnPermissionEnum.values();
        // 添加栏目页（按钮）
        for (CmsSiteColumnPermissionEnum value : values) {
            CmsMenuPO cmsMenuPO = new CmsMenuPO();
            // 生成id
            long id = IdWorker.getId();
            cmsMenuPO.setId(id);
            // 拼接上名称
            String menuName = value.getName();
            cmsMenuPO.setName(menuName);
            // 权限标识
            cmsMenuPO.setPermission(value.getValue());
            // 插入父id、站点id
            cmsMenuPO.setPid(cmsMenuColumnId);
            cmsMenuPO.setPids("[0]," + "[" + cmsMenuSiteId + "]," + "[" + cmsMenuColumnId + "],");
            cmsMenuPO.setSiteId(siteId);
            cmsMenuPO.setCreatorId(userId);
            cmsMenuPO.setModifierId(userId);
            cmsMenuPO.setType(CmsMenuPermissionTypeEnum.AN_NIU.getValue());
            cmsMenuPOS.add(cmsMenuPO);
        }

        // 添加文章（菜单）
        CmsMenuPO cmsMenuArticlePO = new CmsMenuPO();
        long cmsMenuArticleId = IdWorker.getId();
        cmsMenuArticlePO.setId(cmsMenuArticleId);
        cmsMenuArticlePO.setName("文章");
        cmsMenuArticlePO.setPid(cmsMenuSiteId);
        cmsMenuArticlePO.setPids("[0]," + "[" + cmsMenuSiteId + "],");
        cmsMenuArticlePO.setSiteId(siteId);
        cmsMenuArticlePO.setCreatorId(userId);
        cmsMenuArticlePO.setModifierId(userId);
        cmsMenuArticlePO.setType(CmsMenuPermissionTypeEnum.CAI_DAN.getValue());
        cmsMenuPOS.add(cmsMenuArticlePO);

        // 获取站点文章拥有的权限
        CmsSiteArticlePermissionEnum[] articleValues = CmsSiteArticlePermissionEnum.values();
        // 添加文章页（按钮）
        for (CmsSiteArticlePermissionEnum value : articleValues) {
            CmsMenuPO cmsMenuPO = new CmsMenuPO();
            // 生成id
            long id = IdWorker.getId();
            cmsMenuPO.setId(id);
            // 拼接上名称
            String menuName = value.getName();
            cmsMenuPO.setName(menuName);
            // 权限标识
            cmsMenuPO.setPermission(value.getValue());
            // 插入父id、站点id
            cmsMenuPO.setPid(cmsMenuArticleId);
            cmsMenuPO.setPids("[0]," + "[" + cmsMenuSiteId + "]," + "[" + cmsMenuArticleId + "],");
            cmsMenuPO.setSiteId(siteId);
            cmsMenuPO.setCreatorId(userId);
            cmsMenuPO.setModifierId(userId);
            cmsMenuPO.setType(CmsMenuPermissionTypeEnum.AN_NIU.getValue());
            cmsMenuPOS.add(cmsMenuPO);
        }

        // 添加回收站(菜单)
        CmsMenuPO cmsMenuRecoveryPO = new CmsMenuPO();
        long cmsMenuRecoveryId = IdWorker.getId();
        cmsMenuRecoveryPO.setId(cmsMenuRecoveryId);
        cmsMenuRecoveryPO.setName("栏目回收站");
        cmsMenuRecoveryPO.setPid(cmsMenuSiteId);
        cmsMenuRecoveryPO.setPids("[0]," + "[" + cmsMenuSiteId + "],");
        cmsMenuRecoveryPO.setSiteId(siteId);
        cmsMenuRecoveryPO.setCreatorId(userId);
        cmsMenuRecoveryPO.setModifierId(userId);
        cmsMenuRecoveryPO.setType(CmsMenuPermissionTypeEnum.CAI_DAN.getValue());
        cmsMenuPOS.add(cmsMenuRecoveryPO);

        // 回收站页面（操作按钮）
        CmsSiteRecoveryPerMissionEnum[] recoveryValues = CmsSiteRecoveryPerMissionEnum.values();
        for (CmsSiteRecoveryPerMissionEnum value : recoveryValues) {
            CmsMenuPO cmsMenuPO = new CmsMenuPO();
            // 生成id
            long id = IdWorker.getId();
            cmsMenuPO.setId(id);
            // 拼接上名称
            String menuName = value.getName();
            cmsMenuPO.setName(menuName);
            // 权限标识
            cmsMenuPO.setPermission(value.getValue());
            // 插入父id、站点id
            cmsMenuPO.setPid(cmsMenuRecoveryId);
            cmsMenuPO.setPids("[0]," + "[" + cmsMenuSiteId + "]," + "[" + cmsMenuRecoveryId + "],");
            cmsMenuPO.setSiteId(siteId);
            cmsMenuPO.setCreatorId(userId);
            cmsMenuPO.setModifierId(userId);
            cmsMenuPO.setType(CmsMenuPermissionTypeEnum.AN_NIU.getValue());
            cmsMenuPOS.add(cmsMenuPO);
        }

        // 模型(菜单)
        CmsMenuPO cmsMenuModelPO = new CmsMenuPO();
        long cmsMenuModelId = IdWorker.getId();
        cmsMenuModelPO.setId(cmsMenuModelId);
        cmsMenuModelPO.setName("模型");
        cmsMenuModelPO.setPid(cmsMenuSiteId);
        cmsMenuModelPO.setPids("[0]," + "[" + cmsMenuSiteId + "],");
        cmsMenuModelPO.setSiteId(siteId);
        cmsMenuModelPO.setCreatorId(userId);
        cmsMenuModelPO.setModifierId(userId);
        cmsMenuModelPO.setType(CmsMenuPermissionTypeEnum.CAI_DAN.getValue());
        cmsMenuPOS.add(cmsMenuModelPO);

        // 模型页面（操作按钮）
        CmsSiteModelPerMissionEnum[] modelValues = CmsSiteModelPerMissionEnum.values();
        for (CmsSiteModelPerMissionEnum value : modelValues) {
            CmsMenuPO cmsMenuPO = new CmsMenuPO();
            // 生成id
            long id = IdWorker.getId();
            cmsMenuPO.setId(id);
            // 拼接上名称
            String menuName = value.getName();
            cmsMenuPO.setName(menuName);
            // 权限标识
            cmsMenuPO.setPermission(value.getValue());
            // 插入父id、站点id
            cmsMenuPO.setPid(cmsMenuModelId);
            cmsMenuPO.setPids("[0]," + "[" + cmsMenuSiteId + "]," + "[" + cmsMenuModelId + "],");
            cmsMenuPO.setSiteId(siteId);
            cmsMenuPO.setCreatorId(userId);
            cmsMenuPO.setModifierId(userId);
            cmsMenuPO.setType(CmsMenuPermissionTypeEnum.AN_NIU.getValue());
            cmsMenuPOS.add(cmsMenuPO);
        }

        // 模板(菜单)
        CmsMenuPO cmsMenuTemplatePO = new CmsMenuPO();
        long cmsMenuTemplateId = IdWorker.getId();
        cmsMenuTemplatePO.setId(cmsMenuTemplateId);
        cmsMenuTemplatePO.setName("模板");
        cmsMenuTemplatePO.setPid(cmsMenuSiteId);
        cmsMenuTemplatePO.setPids("[0]," + "[" + cmsMenuSiteId + "],");
        cmsMenuTemplatePO.setSiteId(siteId);
        cmsMenuTemplatePO.setCreatorId(userId);
        cmsMenuTemplatePO.setModifierId(userId);
        cmsMenuTemplatePO.setType(CmsMenuPermissionTypeEnum.CAI_DAN.getValue());
        cmsMenuPOS.add(cmsMenuTemplatePO);

        // 模板页面（操作按钮）
        CmsSiteTemplatePermissionEnum[] templateValues = CmsSiteTemplatePermissionEnum.values();
        for (CmsSiteTemplatePermissionEnum value : templateValues) {
            CmsMenuPO cmsMenuPO = new CmsMenuPO();
            // 生成id
            long id = IdWorker.getId();
            cmsMenuPO.setId(id);
            // 拼接上名称
            String menuName = value.getName();
            cmsMenuPO.setName(menuName);
            // 权限标识
            cmsMenuPO.setPermission(value.getValue());
            // 插入父id、站点id
            cmsMenuPO.setPid(cmsMenuTemplateId);
            cmsMenuPO.setPids("[0]," + "[" + cmsMenuSiteId + "]," + "[" + cmsMenuTemplateId + "],");
            cmsMenuPO.setSiteId(siteId);
            cmsMenuPO.setCreatorId(userId);
            cmsMenuPO.setModifierId(userId);
            cmsMenuPO.setType(CmsMenuPermissionTypeEnum.AN_NIU.getValue());
            cmsMenuPOS.add(cmsMenuPO);
        }
        boolean saveBatch = this.saveBatch(cmsMenuPOS);
        return cmsMenuPOS;

    }


    /**
     * 回收站点的菜单权限
     * @param siteIdList
     * @return
     */
    @Override
    public boolean removeSitePermission(ArrayList<Long> siteIdList) {
        UpdateWrapper<CmsMenuPO> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("site_id", siteIdList);
        updateWrapper.set("is_recovery", 1);
        return this.update(updateWrapper);
    }

    /**
     * 恢复站点的菜单权限
     * @param siteIdList
     * @return
     */
    @Override
    public boolean recoverySitePermission(ArrayList<Long> siteIdList) {
        UpdateWrapper<CmsMenuPO> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("site_id", siteIdList);
        updateWrapper.set("is_recovery", 0);
        return this.update(updateWrapper);
    }


    /**
     * 添加栏目的菜单权限
     * @param columnId
     * @param columnName
     * @param siteId
     * @param parentId
     * @param columnXpath
     * @return
     */
    @Override
    public ArrayList<CmsMenuPO> addColumnPermission(Long columnId, String columnName, Long siteId, Long parentId, String columnXpath, Long userId) {

        ArrayList<CmsMenuPO> cmsMenuPOS = new ArrayList<>();

        // 查询所属站点信息
        SitePO sitePO = siteMapper.selectById(siteId);
        if (null == sitePO) {
            return cmsMenuPOS;
        }
        // 查询所属站点的权限菜单
        QueryWrapper<CmsMenuPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CmsMenuPO::getSiteId, siteId);
        queryWrapper.lambda().eq(CmsMenuPO::getPid, 0);
        CmsMenuPO menuSitePO = this.getOne(queryWrapper, false);

        ArrayList<CmsMenuPO> cmsParentMenuPOS = new ArrayList<>();

        String pidsTemp = "";
        Long pid = 0L;

        // 判断是否是顶级栏目----是
        if (null == parentId || parentId.equals(0L)) {

            pidsTemp = "[0]," + "[" + menuSitePO.getId() + "],";
            pid = menuSitePO.getId();
            cmsParentMenuPOS.add(menuSitePO);

        } else {
            // 如果不是顶级站点,则查询其父栏目对应的菜单权限对象
            QueryWrapper<CmsMenuPO> poQueryWrapper = new QueryWrapper<>();
            poQueryWrapper.lambda().eq(CmsMenuPO::getColumnId, parentId);
            poQueryWrapper.lambda().eq(CmsMenuPO::getType, CmsMenuPermissionTypeEnum.LAN_MU.getValue());

            CmsMenuPO one = this.getOne(poQueryWrapper, false);

            // 设置pid和pidsTemp
            pidsTemp = one.getPid() + "[" + one.getId() + "],";

            pid = one.getId();

//            // 获取父级菜单的ID集合
//            String pids = one.getPids();
//            String[] split = pids.substring(0, pids.lastIndexOf(",")).split(",");
//            for (String str : split) {
//                if (str.equals("[0]")) continue;
//                String substring = str.substring(str.indexOf("[") + 1, str.lastIndexOf("]"));
//                CmsMenuPO cmsMenuPO = new CmsMenuPO();
//                cmsMenuPO.setId(Long.valueOf(substring));
//                cmsParentMenuPOS.add(cmsMenuPO);
//            }
//            cmsParentMenuPOS.add(one);
//            cmsMenuPOS.addAll(cmsParentMenuPOS);
        }

        // 栏目名称（目录）
        CmsMenuPO cmsMenuColumnPO = new CmsMenuPO();
        long columnMenuId = IdWorker.getId();
        cmsMenuColumnPO.setId(columnMenuId);
        cmsMenuColumnPO.setName(columnName);
        // pid
        cmsMenuColumnPO.setPid(pid);
        // pids
        cmsMenuColumnPO.setPids(pidsTemp);
        cmsMenuColumnPO.setColumnId(columnId);
        cmsMenuColumnPO.setSiteId(siteId);
        cmsMenuColumnPO.setCreatorId(userId);
        cmsMenuColumnPO.setModifierId(userId);
        cmsMenuColumnPO.setType(CmsMenuPermissionTypeEnum.LAN_MU.getValue());
        cmsMenuPOS.add(cmsMenuColumnPO);

        // 子栏目(菜单)
        CmsMenuPO cmsMenuChildColumnPO = new CmsMenuPO();
        long cmsMenuChildColumnId = IdWorker.getId();
        cmsMenuChildColumnPO.setId(cmsMenuChildColumnId);
        cmsMenuChildColumnPO.setName("栏目");
        // pid
        cmsMenuChildColumnPO.setPid(columnMenuId);
        // pids
        cmsMenuChildColumnPO.setPids(pidsTemp + "[" + columnMenuId + "],");
        cmsMenuChildColumnPO.setColumnId(columnId);
        cmsMenuChildColumnPO.setSiteId(siteId);
        cmsMenuChildColumnPO.setCreatorId(userId);
        cmsMenuChildColumnPO.setModifierId(userId);
        cmsMenuChildColumnPO.setType(CmsMenuPermissionTypeEnum.CAI_DAN.getValue());
        cmsMenuPOS.add(cmsMenuChildColumnPO);

        // 子栏目页(按钮)
        CmsColumnChildPermissionEnum[] values = CmsColumnChildPermissionEnum.values();
        for (CmsColumnChildPermissionEnum value : values) {
            CmsMenuPO cmsMenuPO = new CmsMenuPO();
            // 生成id
            long id = IdWorker.getId();
            cmsMenuPO.setId(id);
            // 拼接上名称
            String menuName = value.getName();
            cmsMenuPO.setName(menuName);
            // 权限标识
            cmsMenuPO.setPermission(value.getValue());
            // 插入父id、站点id
            cmsMenuPO.setPid(cmsMenuChildColumnId);
            // 父ids
            String pids = pidsTemp + "[" + columnMenuId + "]," + "[" + cmsMenuChildColumnId + "],";
            cmsMenuPO.setPids(pids);
            // 站点id
            cmsMenuPO.setSiteId(siteId);
            cmsMenuPO.setCreatorId(userId);
            cmsMenuPO.setModifierId(userId);
            // 栏目id
            cmsMenuPO.setColumnId(columnId);
            cmsMenuPO.setType(CmsMenuPermissionTypeEnum.AN_NIU.getValue());
            cmsMenuPOS.add(cmsMenuPO);
        }

        // 文章（菜单）
        CmsMenuPO cmsMenuArticlePO = new CmsMenuPO();
        long cmsMenuArticleId = IdWorker.getId();
        cmsMenuArticlePO.setId(cmsMenuArticleId);
        cmsMenuArticlePO.setName("文章");
        // pid
        cmsMenuArticlePO.setPid(columnMenuId);
        // pids
        cmsMenuArticlePO.setPids(pidsTemp + "[" + columnMenuId + "],");
        cmsMenuArticlePO.setColumnId(columnId);
        cmsMenuArticlePO.setSiteId(siteId);
        cmsMenuArticlePO.setCreatorId(userId);
        cmsMenuArticlePO.setModifierId(userId);
        cmsMenuArticlePO.setType(CmsMenuPermissionTypeEnum.CAI_DAN.getValue());
        cmsMenuPOS.add(cmsMenuArticlePO);

        // 文章页(按钮)
        CmsColumnArticlePermissionEnum[] articleValues = CmsColumnArticlePermissionEnum.values();
        for (CmsColumnArticlePermissionEnum value : articleValues) {
            CmsMenuPO cmsMenuPO = new CmsMenuPO();
            // 生成id
            long id = IdWorker.getId();
            cmsMenuPO.setId(id);
            // 拼接上名称
            String menuName = value.getName();
            cmsMenuPO.setName(menuName);
            // 权限标识
            cmsMenuPO.setPermission(value.getValue());
            // 插入父id、站点id
            cmsMenuPO.setPid(cmsMenuArticleId);
            // 父ids
            String pids = pidsTemp + "[" + columnMenuId + "]," + "[" + cmsMenuArticleId  + "],";
            cmsMenuPO.setPids(pids);
            // 站点id
            cmsMenuPO.setSiteId(siteId);
            cmsMenuPO.setCreatorId(userId);
            cmsMenuPO.setModifierId(userId);
            // 栏目id
            cmsMenuPO.setColumnId(columnId);
            cmsMenuPO.setType(CmsMenuPermissionTypeEnum.AN_NIU.getValue());
            cmsMenuPOS.add(cmsMenuPO);
        }

        // 添加回收站(菜单)
        CmsMenuPO cmsMenuRecoveryPO = new CmsMenuPO();
        long cmsMenuRecoveryId = IdWorker.getId();
        cmsMenuRecoveryPO.setId(cmsMenuRecoveryId);
        cmsMenuRecoveryPO.setName("回收站");
        cmsMenuRecoveryPO.setPid(columnMenuId);
        cmsMenuRecoveryPO.setPids(pidsTemp + "[" + columnMenuId + "],");
        cmsMenuRecoveryPO.setSiteId(siteId);
        cmsMenuRecoveryPO.setCreatorId(userId);
        cmsMenuRecoveryPO.setModifierId(userId);
        cmsMenuRecoveryPO.setColumnId(columnId);
        cmsMenuRecoveryPO.setType(CmsMenuPermissionTypeEnum.CAI_DAN.getValue());
        cmsMenuPOS.add(cmsMenuRecoveryPO);

        // 回收站页面（操作按钮）
        CmsColumnRecoveryPermissionEnum[] recoveryValues = CmsColumnRecoveryPermissionEnum.values();
        for (CmsColumnRecoveryPermissionEnum value : recoveryValues) {
            CmsMenuPO cmsMenuPO = new CmsMenuPO();
            // 生成id
            long id = IdWorker.getId();
            cmsMenuPO.setId(id);
            // 拼接上名称
            String menuName = value.getName();
            cmsMenuPO.setName(menuName);
            // 权限标识
            cmsMenuPO.setPermission(value.getValue());
            // 插入父id、站点id
            cmsMenuPO.setPid(cmsMenuRecoveryId);
            cmsMenuPO.setCreatorId(userId);
            cmsMenuPO.setModifierId(userId);
            // 父ids
            String pids = pidsTemp + "[" + columnMenuId + "]," + "[" + cmsMenuRecoveryId  + "],";
            cmsMenuPO.setPids(pids);
            // 站点id
            cmsMenuPO.setSiteId(siteId);
            // 栏目id
            cmsMenuPO.setColumnId(columnId);
            cmsMenuPO.setType(CmsMenuPermissionTypeEnum.AN_NIU.getValue());
            cmsMenuPOS.add(cmsMenuPO);
        }

        boolean saveMenu = this.saveBatch(cmsMenuPOS);

        return cmsMenuPOS;
    }


    /**
     * 回收栏目的菜单权限
     * @param columnIdList
     * @return
     */
    @Override
    public boolean removeColumnPermission(ArrayList<Long> columnIdList) {
        // 查询出菜单表中类型为栏目的权限的id
        QueryWrapper<CmsMenuPO> cmsMenuPOQueryWrapper = new QueryWrapper<>();
        cmsMenuPOQueryWrapper.select("ID");
        cmsMenuPOQueryWrapper.in("COLUMN_ID", columnIdList);
        cmsMenuPOQueryWrapper.eq("TYPE", "LAN_MU");
        List<CmsMenuPO> list = this.list(cmsMenuPOQueryWrapper);

        ArrayList<Long> menuIdList = new ArrayList<>();
        for (CmsMenuPO cmsMenuPO : list) {
            menuIdList.add(cmsMenuPO.getId());
        }

        // 1、回收栏目对应的菜单权限-----栏目菜单
        UpdateWrapper<CmsMenuPO> updateSelfWrapper = new UpdateWrapper<>();
        updateSelfWrapper.in("ID", menuIdList);
        updateSelfWrapper.set("is_recovery", 1);
        boolean updateSelf = this.update(updateSelfWrapper);
        if (!updateSelf) {
            return false;
        }

        // 2、回收子栏目对应的菜单权限-----子栏目菜单和按钮
        for (Long menuId : menuIdList) {
            UpdateWrapper<CmsMenuPO> updateChildWrapper = new UpdateWrapper<>();
            updateChildWrapper.like("PIDS", menuId);
            updateChildWrapper.set("is_recovery", 1);
            boolean updateChild = this.update(updateChildWrapper);

            if (!updateChild) {
                return false;
            }
        }
        return true;

    }

    /**
     * 菜单权限树
     * @return
     */
    @Override
    public CmsMenuPermissionListVO permissionTree() {

        // 获取所有的站点菜单权限记录
        QueryWrapper<CmsMenuPO> querySiteMenuWrapper = new QueryWrapper<>();
        querySiteMenuWrapper.lambda().eq(CmsMenuPO::getRecovery, 0);
        querySiteMenuWrapper.lambda().eq(CmsMenuPO::getType, "ZHAN_DIAN");
        List<CmsMenuPO> siteMenuList = this.list(querySiteMenuWrapper);

        // 获取所有未被回收的菜单权限记录
        QueryWrapper<CmsMenuPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CmsMenuPO::getRecovery, 0);
        List<CmsMenuPO> AllMenuList = this.list(queryWrapper);

        // 获取菜单栏目树集合
        ArrayList<CmsMenuPermissionTreeVO> cmsMenuPermissionTreeVOS = new ArrayList<>();
        // 首先遍历站点菜单获取其子菜单
        for (CmsMenuPO cmsMenuPO : siteMenuList) {
            CmsMenuPermissionTreeVO cmsMenuPermissionTreeVO = p_menuTree(cmsMenuPO, AllMenuList);
            cmsMenuPermissionTreeVOS.add(cmsMenuPermissionTreeVO);
        }
        CmsMenuPermissionListVO cmsMenuPermissionListVO = new CmsMenuPermissionListVO();
        cmsMenuPermissionListVO.setList(cmsMenuPermissionTreeVOS);

        return cmsMenuPermissionListVO;
    }

    // 整理菜单权限树
    private CmsMenuPermissionTreeVO p_menuTree(CmsMenuPO cmsMenuPO, List<CmsMenuPO> AllMenuList) {
        // 先将当前父菜单整理为VO
        CmsMenuPermissionTreeVO cmsParentTreeVO = new CmsMenuPermissionTreeVO();
        cmsParentTreeVO.setId(cmsMenuPO.getId());
        cmsParentTreeVO.setName(cmsMenuPO.getName());
        cmsParentTreeVO.setPid(cmsMenuPO.getPid());
        cmsParentTreeVO.setType(cmsMenuPO.getType());

        // 存储子菜单的集合
        ArrayList<CmsMenuPermissionTreeVO> cmsMenuPermissionTreeVOS = new ArrayList<>();
        for (CmsMenuPO menuPO : AllMenuList) {
            // 如果子菜单的pid等于当前菜单的id
            if (menuPO.getPid().equals(cmsMenuPO.getId())) {
                // 则查询此子菜单的子菜单
                CmsMenuPermissionTreeVO cmsMenuPermissionTreeVO = p_menuTree(menuPO, AllMenuList);
                // 返回值将其添加到集合中
                cmsMenuPermissionTreeVOS.add(cmsMenuPermissionTreeVO);
            }
        }
        cmsParentTreeVO.setChildList(cmsMenuPermissionTreeVOS);
        return cmsParentTreeVO;
    }


    @Override
    public List<CmsMenuPO> getAllRolePermission(ArrayList<Long> roleList, String type) {
        QueryWrapper cmsMenuPOQueryWrapper = new QueryWrapper<>();
        cmsMenuPOQueryWrapper.eq("CMS_MENU.IS_DELETED", 0);
        if (null != type) {
            cmsMenuPOQueryWrapper.eq("CMS_MENU.TYPE", type);
        }
        cmsMenuPOQueryWrapper.in("CMS_ROLE_MENU.ROLE_ID", roleList);
        List<CmsMenuPO> cmsMenuPOS = baseMapper.selectRoleMenu(cmsMenuPOQueryWrapper);
        return cmsMenuPOS;
    }
}
